"""
pip install pyjwt
"""
import jwt
import datetime
import time
from mdpro.settings import SECRET_KEY


def create_token(user_id):
    # payload
    payload = {
        'user_id': user_id,
        'exp': datetime.datetime.utcnow() + datetime.timedelta(seconds=900)
    }
    token = jwt.encode(payload, key=SECRET_KEY).decode()
    return token


def login_token(user_id):
    # payload
    payload = {
        'user_id': user_id,
        'exp': datetime.datetime.utcnow() + datetime.timedelta(days=1)
    }
    token = jwt.encode(payload, key=SECRET_KEY).decode()
    return token


def check_token(token):
    try:
        user_info = jwt.decode(token, key=SECRET_KEY)
    except jwt.exceptions.ExpiredSignatureError:
        return None
    return user_info


if __name__ == '__main__':
    token = create_token('1')

    print(check_token(token))
